﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ClockSync
{
    public class TcpHandler
    {
        public TcpClient tcpclnt;
        public IPAddress IPtoSendTo;
        public int port;
        public Socket socket;
        IPAddress ipAddressToGetFrom;
        NetworkStream clientStream;
        TcpClient client;

        private TcpListener tcpListener;
        private Thread listenThread;

        public TcpHandler(IPAddress ip_to_send_to, int port_to_send_to, int port_to_get_from)
        {
            tcpClient(ip_to_send_to, port_to_send_to);
            tcpServer(IPAddress.Any, port_to_get_from);
        }

        public void tcpServer(IPAddress ipAddress, int port)
        {
            this.tcpListener = new TcpListener(IPAddress.Any, port);
            this.tcpListener.Start();

        }


        public byte[] getMsg()
        {

            this.client = this.tcpListener.AcceptTcpClient();
            this.clientStream = this.client.GetStream();

            TcpClient client = this.tcpListener.AcceptTcpClient();

            NetworkStream clientStream = client.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                //ASCIIEncoding encoder = new ASCIIEncoding();
               // System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
            }

            return message;
          
        }


        public void exit()
        {
            tcpListener.Stop();
            socket.Close();
        }

        //===================================================================================================

        public void tcpClient(IPAddress ip, int port)
        {
            try
            {
                tcpclnt = new TcpClient();
                Console.WriteLine("tcpClient, connecting...");
                tcpclnt.Connect(ip, port); // use the ipaddress as in the server program
                this.IPtoSendTo = ip;
                this.port = port;
                Console.WriteLine("connected");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in TCPClient" + e.StackTrace);
            }
        }

        public void open(IPAddress ip, int port)
        {
            Console.WriteLine("in tcpClient constructor ip: " + ip.ToString() + " port: " + port);
            try
            {
                tcpclnt = new TcpClient();
                Console.WriteLine("tcpClient, connecting...");
                tcpclnt.Connect(ip, port); // use the ipaddress as in the server program
                this.IPtoSendTo = ip;
                this.port = port;
                Console.WriteLine("connected");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in open " + e.StackTrace);
            }
        }

        public void exitClient()
        {
            tcpclnt.Close();
        }

        public void sendMsg(String msg)
        {
            Stream stm = tcpclnt.GetStream();
            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] word = asen.GetBytes(msg);
            stm.Write(word, 0, word.Length);
            stm.Flush();
            Console.WriteLine("msg sent");

        }

    }
}